Slide 23 / 41

Step-by-Step Construction

Visualizing the stack's evolution for: 3 4 + 5 * 6 1 1 + / -

T

If token is an Operand:

Create a single-node tree and push it onto the stack.

+

If token is an Operator:

Pop two trees, create a new root with the operator, attach them as children, and push the new tree back.

Process Visualization

1. Read '3'

Node('3')

2. Read '4'

Node('3')
Node('4')

3. Read '+'

Tree(root='+')

4. Read '5'

Tree(root='+')
Node('5')

5. Read '*'

Tree(root='*')

6. Read '6'

Tree(root='*')
Node('6')

7. Read '1'

Tree(root='*')
Node('6')
Node('1')

8. Read '1'

Tree(root='*')
Node('6')
Node('1')
Node('1')

9. Read '+'

Tree(root='*')
Node('6')
Tree(root='+')

10. Read '/'

Tree(root='*')
Tree(root='/')

11. Read '-'

Tree(root='-')

Final Result